﻿using System;
using System.Windows.Forms;
using l1cs.server;
using System.Threading;
using l1cs.server.utils;
using l1cs.server.datatables;
using l1cs.server.model.gametime;
using l1cs.server.server.utils;
using l1cs.server.encryptions;
using l1cs.server.model;

namespace l1cs
{
    public partial class frmMain : Form
    {
        public static bool isEncode;

        private Thread gameThread;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load( object sender, EventArgs e )
        {
            isEncode = chkEncode.Checked;

            // 設定の読込み
            Config.load();

            stlJokyo.Text = "設定ファイルの読込完了";

            txtExpRate.Text = Config.RATE_XP.ToString();
            txtLawfulRate.Text = Config.RATE_LA.ToString();
            txtKarmaRate.Text = Config.RATE_KARMA.ToString();
            txtDropAdenaRate.Text = Config.RATE_DROP_ADENA.ToString();
            txtDropItemsRate.Text = Config.RATE_DROP_ITEMS.ToString();
            txtAllChat.Text = Config.GLOBAL_CHAT_LEVEL.ToString();
            if ( Config.ALT_NONPVP )
            { // Non-PvP設定
                txtPvP.Text = "PvP可能";
            }
            else
            {
                txtPvP.Text = "PvP不可";
            }
            txtMaxCon.Text = Config.MAX_ONLINE_USERS.ToString();

            // Databaseの設定
            MySqlWrapper.setDatabaseSettings( Config.DB_HOST, Config.DB_NAME, Config.DB_LOGIN, Config.DB_PASSWORD );

            //if (Config.TELNET_SERVER)
            //{
            //    TelnetServer.getInstance().start();
            //}

            //2003年7月3日 12:00(UTC)
            DateTime l1start = DateTime.Parse( "2003/07/03 12:00:00" ); //DateTime.Now.ToUniversalTime();
            Console.WriteLine( "2003/07/03 12:00:00 = {0},{1}(100u秒単位)", l1start, l1start.Ticks );
            l1start = DateTime.Parse( "2003/07/03 13:00:00" );
            Console.WriteLine( "2003/07/03 13:00:00 = {0},{1}(100u秒単位)", l1start, l1start.Ticks );
            DateTime date = new DateTime();
            date = date.AddMilliseconds( 1 );
            Console.WriteLine( date.Ticks );
            DateTime dateTime1 = DateTime.Parse( "2003/07/03 12:00:00" );
            DateTime dateTime2 = DateTime.UtcNow;
            TimeSpan timeSpan = dateTime2 - dateTime1;
            Console.WriteLine( timeSpan.Ticks );
            if ( false )
            {
                // ゲーム時間計算タイムアタック
                int count = 100000;
                long _time;
                DateTime _calendar;
                PerformanceTimer start = new PerformanceTimer();
                Console.WriteLine( DateTime.Now.ToString() );
                start.reset();
                for ( int j = 0; j < 100; j++ )
                {
                    for ( int i = 0; i < count; i++ )
                    {
                        long t1 = DateTime.Now.ToUniversalTime().Ticks - 631928304000000000L;
                        if ( t1 < 0 )
                        {
                            throw new ArgumentNullException();
                        }
                        long t2 = ( t1 * 6 ) / 10000000L;
                        long t3 = t2 % 3; // 時間が3の倍数になるように調整
                        _time = t2 - t3;
                        _calendar = new DateTime( 0 ).AddSeconds( t2 - t3 );
                        //if ( i == 0 )
                        //    _log.info( _calendar.ToString() );
                    }
                    long java = start.get();
                    //_log.info( "java\t" + start.get() + "㍉秒" );
                    DateTime baseTime = new DateTime( 0 );
                    start.reset();
                    for ( int i = 0; i < count; i++ )
                    {
                        //long t1 = DateTime.Now.ToUniversalTime().Ticks - 631928304000000000L;
                        TimeSpan t1 = DateTime.UtcNow - dateTime1;
                        if ( t1.Ticks < 0 )
                        {
                            throw new ArgumentNullException();
                        }
                        long t2 = (long)( t1.TotalSeconds * 6 );
                        long t3 = t2 % 3; // 時間が3の倍数になるように調整
                        _time = t2 - t3;
                        _calendar = new DateTime().AddSeconds( t2 - t3 );
                        //if ( i == 0 )
                        //    _log.info( _calendar.ToString() );
                    }
                    Console.WriteLine( "java\t" + java + "\tC#\t" + start.get() );
                }
            }
            //
            object socket = new object();
            long seed = 0x7c98bdfa;
            LineageKeys _clkey = LineageEncryption.initKeys( socket, seed );
            //
            Console.WriteLine( String.Format( "利用メモリ: {0:f2}MB", GC.GetTotalMemory( false ) / 1024.0 / 1024.0 ) );
        }

        private void btnStart_Click( object sender, EventArgs e )
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            gameThread = GameServer.getInstance().initialize();

            stlJokyo.Text = "ゲームサーバ起動完了";
        }

        private void chkEncode_CheckedChanged( object sender, EventArgs e )
        {
            isEncode = chkEncode.Checked;
        }

        private void frmMain_FormClosing( object sender, FormClosingEventArgs e )
        {
            // フォームの位置等の情報を保存
            Properties.Settings.Default.Save();
            GameServer.getInstance().socketClose();
            if ( gameThread != null )
            {
                gameThread.Abort();
            }
        }

        private void btnStop_Click( object sender, EventArgs e )
        {
            GameServer.getInstance().socketClose();
            if ( gameThread != null )
            {
                gameThread.Abort();
            }
        }
    }
}
